module planegame{

    export class EnemyBullet extends Bullet {

        private bulletOffset;
        private bulletFlyTime;

        private bulletOffsetFinish;

        private speed;

        private game;
        public constructor(resName:string, game:Game) {
            super();
            this.bulletNum = 3;
            let res = RES.getRes("description_json");
            this.bulletOffset = res.bulletOffset.enemy;
            this.bulletFlyTime = res.bulletFlyTime.enemy;
            this.bulletOffsetFinish = res.bulletOffsetFinish.enemy;
            this.resName = resName;
            this.game = game;

        }

        public fire() {
            
            let plane:Plane = <Plane>this.parent;
            let game:Game = <Game>plane.parent;
            let enemy:Plane = <Plane>game.getChildByName("plane");

            //let bulletLength = Math.sqrt(this.stage.stageWidth*this.stage.stageWidth + this.stage.stageHeight*this.stage.stageHeight) 

            let bulletLength = 1000;
            for(let i:number = 0;i<this.bulletNum;i++) {
                let v = this.getBullet(this.resName);
                
                v.x = plane.x + this.bulletOffset[i].x;
                v.y = plane.y - v.height/2 + this.bulletOffset[i].y;

                game.addChild(v);
                plane.bulletArray.push(v);

                v.k = (enemy.y - v.y) / (enemy.x - v.x);
                if(enemy.x - v.x < 0 ) {
                    v.speed = -v.speed;
                }
/*
                let finalX:number = 0;
                let finalY:number = 0;
                if(enemy.x - v.x >= 0) {
                    finalX = v.x + bulletLength;
                } else {
                    finalX = v.x - bulletLength;
                }

                finalY = (enemy.y - v.y) / (enemy.x - v.x) * finalX + v.y;

                egret.Tween.get(v).to({x:finalX, y:finalY}, this.bulletFlyTime).call(function() {
                    let index = game.enemyBulletArray.indexOf(v);
                    if(index != -1) {
                        game.enemyBulletArray.splice(index, 1);
                    }
                    if(game.contains(v)) {
                        game.removeChild(v);
                    }
                }, this);
*/
            }
           
        }

        public removeBullet(v:BulletModule) {
            let plane:Plane = <Plane>this.parent;

            let index = plane.bulletArray.indexOf(v);
            if(index != -1) {
                plane.bulletArray.splice(index, 1);
            }
            if(this.game.contains(v)) {
                this.game.removeChild(v);
            }
        }

        public updateFrame() {
            let plane:Plane = <Plane>this.parent;
            let stageW = GameUtils.instance().getStageWidth();
            let stageH = GameUtils.instance().getStageHeight();

            plane.bulletArray.forEach(function(v, i, a) {
                v.x+=v.speed;
                v.y+=v.speed*v.k;
                // 子弹出墙壁销毁
                if(v.x > stageW || v.y > stageH) {
                    this.removeBullet(v);
                }
            }, this);

        }

    }
}